<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div class="Left">
        <img src="../assets/Capgemini-logo.png">
        <span>项目管理系统</span>
      </div>
      <div class="Right">
        <span>欢迎您：{{ userInfo.userName }}</span>
        <el-row>
          <el-button type="danger" plain @click="logout" size="large">
          <el-icon><SwitchButton /></el-icon>
          </el-button>
        </el-row>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <!-- 侧边栏菜单 -->
        <el-menu :default-active="$route.path"  router background-color="#303030" text-color="#fff">
          <!-- 为侧边栏绑定数据添加循环 -->
          <el-menu-item :index="'/'+item.path" v-for="item in menuList" :key="item.menuId">
            <template #title>
              <span>{{ item.menuName }}</span>
              </template>
          </el-menu-item>

        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { reactive, toRefs } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import axios from 'axios'
export default {
  setup() {
    const router = useRouter()
    const state = reactive({
      // 用户信息对象
      userInfo: null,
      // 侧边栏列表数据
      menuList: []
      // // 侧边栏数据图标
      // iconObj: {
      //   201: Search,
      //   125: 'el-icon-user',
      //   103: 'el-icon-star-off',
      //   101: 'el-icon-goods',
      //   102: 'el-icon-bell'

      // }
    })
    const logout = () => {
      ElMessageBox.confirm(
        '您是否要退出登录？',
        '退出登录',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          ElMessage({
            type: 'success',
            message: '退出成功'
          })
          router.push({ path: '/login' })
        })
    }
    // 获取左侧菜单列表
    async function getMenuList() {
      const { data: res } = await axios.get('/menu')
      // console.log(res)
      // console.log(res.data)
      state.menuList = res.data
      // res.menuName = state.menuList
      // console.log(state.menuList)
      // state.menuList = res.menuName
      // console.log(state.menulist)
    }
    // 调用sessionStorage中的userInfo数据，并转换为对象形式
    const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
    getMenuList()
    return {
      ...toRefs(state),
      logout,
      getMenuList,
      userInfo
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .el-header {
    background-color: #303030;
    height: 60px !important;
    display: flex;
    // 两头对齐
    justify-content: space-between;
    align-items: center;
    color: #fff;
    .Left {
      display: flex;
      align-items: center;
      font-size:24px;
      img{
        height:100px;
      }

    }
    .Right {
      display: flex;
      align-items: center;
      span {
        margin-right: 20px;
      }
    }
  }
  .el-icon-link{
    line-height:20px
  }
  .el-aside {
    background-color:#303030;
    .el-menu {
      border-right: 0;
      .iconfont {
        margin-right: 10px;
      }
    }

  }
  .el-main{
    background-color:#F2F6FC
  }
}
</style>
